<?php
/* 视图文件 */
$host = $_SERVER['HTTP_HOST'];

/* file */

$startdir = '.';
$showthumbnails = false;
$showdirs = true;
$forcedownloads = false;
$hide = array(
	'dlf',
	'public_html',
	'.php',
	'.phtml',
	'.htaccess',
	'.html',
	'lists_bak.txt',
	'uniqid.txt',
	'stop.txt'
);
$displayindex = false;
$allowuploads = false;
$overwrite = false;
$indexfiles = array(
	'index.html',
	'index.htm',
	'default.htm',
	'default.html'
);
$filetypes = array(
	'png' => 'jpg.gif',
	'jpeg' => 'jpg.gif',
	'bmp' => 'jpg.gif',
	'jpg' => 'jpg.gif',
	'gif' => 'gif.gif',
	'zip' => 'archive.png',
	'rar' => 'archive.png',
	'exe' => 'exe.gif',
	'setup' => 'setup.gif',
	'txt' => 'text.png',
	'htm' => 'html.gif',
	'html' => 'html.gif',
	'php' => 'php.gif',
	'fla' => 'fla.gif',
	'swf' => 'swf.gif',
	'xls' => 'xls.gif',
	'doc' => 'doc.gif',
	'sig' => 'sig.gif',
	'fh10' => 'fh10.gif',
	'pdf' => 'pdf.gif',
	'psd' => 'psd.gif',
	'rm' => 'real.gif',
	'mpg' => 'video.gif',
	'mpeg' => 'video.gif',
	'mov' => 'video2.gif',
	'avi' => 'video.gif',
	'eps' => 'eps.gif',
	'gz' => 'archive.png',
	'asc' => 'sig.gif',
	'mp3' => 'video.gif',
);
error_reporting(0);
if (!function_exists('imagecreatetruecolor'))
	$showthumbnails = false;
$leadon = $startdir;
if ($leadon == '.')
	$leadon = '';
if ((substr($leadon, -1, 1) != '/') && $leadon != '')
	$leadon = $leadon . '/';
$startdir = $leadon;
if ($_GET['dir']) {
	//check this is okay.
	if (substr($_GET['dir'], -1, 1) != '/') {
		$_GET['dir'] = $_GET['dir'] . '/';
	}
	$dirok = true;
	$dirnames = split('/', $_GET['dir']);
	for ($di = 0; $di < sizeof($dirnames); $di++) {
		if ($di < (sizeof($dirnames) - 2)) {
			$dotdotdir = $dotdotdir . $dirnames[$di] . '/';
		}
		if ($dirnames[$di] == '..') {
			$dirok = false;
		}
	}
	if (substr($_GET['dir'], 0, 1) == '/') {
		$dirok = false;
	}
	if ($dirok) {
		$leadon = $leadon . $_GET['dir'];
	}
}
$opendir = $leadon;
if (!$leadon)
	$opendir = '.';
if (!file_exists($opendir)) {
	$opendir = '.';
	$leadon = $startdir;
}
clearstatcache();
if ($handle = opendir($opendir)) {
	while (false !== ($file = readdir($handle))) {
		//first see if this file is required in the listing
		if ($file == "." || $file == "..")
			continue;
		$discard = false;
		for ($hi = 0; $hi < sizeof($hide); $hi++) {
			if (strpos($file, $hide[$hi]) !== false) {
				$discard = true;
			}
		}
		if ($discard)
			continue;
		if (@filetype($leadon . $file) == "dir") {
			if (!$showdirs)
				continue;
			$n++;
			if ($_GET['sort'] == "date") {
				$key = @filemtime($leadon . $file) . ".$n";
			} else {
				$key = $n;
			}
			$dirs[$key] = rawurlencode($file) . "/"; // 转码
		} else {
			$n++;
			if ($_GET['sort'] == "date") {
				$key = @filemtime($leadon . $file) . ".$n";
			} elseif ($_GET['sort'] == "size") {
				$key = @filesize($leadon . $file) . ".$n";
			} else {
				$key = $n;
			}
			$files[$key] = $file;
			// 图片集合
			if (preg_match('/\.(?:jpg|png)$/i', $file)) {
				$pics_arr[] = "'" . $file . "'";
			}
			if ($displayindex) {
				if (in_array(strtolower($file), $indexfiles)) {
					header("Location: $file");
					die();
				}
			}
		}
	}
	
	// 图片集合
	$pics = '[]';
	if ($pics_arr) {
		@natcasesort($pics_arr);
		$pics_str = implode(',', $pics_arr);
		$pics = '[' . $pics_str . ']';
		#### 下一话
		///yuekanshaonvyeqijun/XDM_0001_vol_01/
		$path0 = $_SERVER['PHP_SELF'];
		$parentDir = 'http://' . $_SERVER['HTTP_HOST'] . preg_replace('/\/([^\/]+\/)([^\/]+)\/index.php$/U', '/\\1index.php?getNext=\\2', $path0);
		//preg_match('/\/[^\/]+\/([^\/]+)\/$/U', $path0, $volPathArr);
		//$volPath = $volPathArr[1];
		$dirNext = file_get_contents($parentDir);
		$nextPath = '';
		if ($dirNext != '') {
			$nextPath = preg_replace('/(\/[^\/]+\/)([^\/]+)\/index.php$/U', "\\1" . rawurlencode($dirNext) . "/", $path0);
		}
	}
	closedir($handle);
}
//sort our files
if ($_GET['sort'] == "date") {
	@ksort($dirs, SORT_NUMERIC);
	@ksort($files, SORT_NUMERIC);
} elseif ($_GET['sort'] == "size") {
	@natcasesort($dirs);
	@ksort($files, SORT_NUMERIC);
} else {
	@natcasesort($dirs);
	@natcasesort($files);
}
#### 获取下一卷

if ($_GET['getNext'] != '' && $dirs) {
	$nextDir = FALSE;
	foreach ($dirs AS $di) {
		//echo $di . '<br>';
		$nextDir == TRUE && die(rawurlencode(trim($di, '/')));
		rawurldecode($di) == rawurldecode($_GET['getNext']) . '/' && $nextDir = TRUE;
	}
	die;
}
//order correctly
if ($_GET['order'] == "desc" && $_GET['sort'] != "size") {
	$dirs = @array_reverse($dirs);
}
if ($_GET['order'] == "desc") {
	$files = @array_reverse($files);
}
$dirs = @array_values($dirs);
$files = @array_values($files);
?>
<!DOCTYPE html PUBLIC '-//WAPFORUM//DTD XHTML Mobile 1.0//EN' 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd'>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta content="text/html; charset=utf-8" http-equiv="content-type" />
		<title>{hostTitle} Comic downloader</title>
		<link rel="stylesheet" type="text/css" href="{view.css}" />
		<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
	</head>
	<body>
		<style type="text/css">
		</style>
		<body>
			<div id="container">
				<h1><a id="comicDir" href="{comicDir}/?">漫画仓库</a>　<a id="_back" href="javascript:history.go(-1);">后退</a></h1>
				<div id="breadcrumbs">
					<a href="{homeUrl}/?">返回主页</a>　　<?php if ($pics_arr) { ?><a href="javascript:xindm.switchView();">阅读漫画>></a><?php } ?>
				</div>
				<div id="listingcontainer">
					<div id="listingheader">
						<div id="headerfile">File</div>
						<div id="headersize">Size</div>
						<div id="headermodified">Modified</div>
					</div>
					<div id="listing">
						<?php
						$class = 'b';
						if ($dirok) {
							?>
							<div><a href="<?php echo $dotdotdir; ?>" class="<?php echo $class; ?>"><img src="http://cpanel.main-hosting.com/images/index/dirup.png" alt="Folder" /><strong>..</strong> <em>-</em>
									<?php echo date("M d Y h:i:s A", filemtime($dotdotdir)); ?>
								</a></div>
							<?php
							if ($class == 'b')
								$class = 'w';
							else
								$class = 'b';
						}
						$arsize = sizeof($dirs);
						for ($i = 0; $i < $arsize; $i++) {
							?>
							<div><a href="<?php echo $leadon . $dirs[$i]; ?>" class="<?php echo $class; ?>"><img src="http://cpanel.main-hosting.com/images/index/folder.png" alt="<?php echo $dirs[$i]; ?>" /><strong>
										<?php echo $dirs[$i]; ?>
									</strong> <em>-</em>
									<?php echo date("M d Y h:i:s A", filemtime($leadon . $dirs[$i])); ?>
								</a></div>
							<?php
							if ($class == 'b')
								$class = 'w';
							else
								$class = 'b';
						}
						$arsize = sizeof($files);
						for ($i = 0; $i < $arsize; $i++) {
							$icon = 'unknown.png';
							$ext = strtolower(substr($files[$i], strrpos($files[$i], '.') + 1));
							$supportedimages = array('gif', 'png', 'jpeg', 'jpg');
							$thumb = '';
							if ($filetypes[$ext]) {
								$icon = $filetypes[$ext];
							}
							$filename = $files[$i];
							if (strlen($filename) > 43) {
								$filename = substr($files[$i], 0, 40) . '...';
							}
							$fileurl = $leadon . $files[$i];
							?>
							<div><a href="<?php echo $fileurl; ?>" class="<?php echo $class; ?>"<?php echo $thumb2; ?>><img src="http://cpanel.main-hosting.com/images/index/<?php echo $icon; ?>" alt="<?php echo $files[$i]; ?>" /><strong>
										<?php echo $filename; ?>
									</strong> <em>
										<?php echo round(filesize($leadon . $files[$i]) / 1024); ?>
										KB</em>
									<?php echo date("M d Y h:i:s A", filemtime($leadon . $files[$i])); ?>
									<?php echo $thumb; ?>
								</a></div>
							<?php
							if ($class == 'b')
								$class = 'w';
							else
								$class = 'b';
						}
						?>
					</div>
				</div>
			</div>
			<div id="picView">

			</div>
			<style>

			</style>
			<script>
				var xindm = {
					"files": <?php echo $pics; ?>,
					"switchView": function() {
						var s = this;
						if (s.files.length > 0) {
							$('#container').hide() + $('#picView').show();
							s.makeImg();
							s.styleSet();
							$(window).resize(function() {
								s.styleSet();
							});
						}
					},
					"makeImg": function() {
						var nextPath = "<?php echo $nextPath; ?>";
						var s = this;
						var img_arr = [];
						for (var i in s.files) {
							var img = '<div class="picBox"><img src="' + s.files[i] + '" /></div>';
							img_arr.push(img);
						}
						var imgHtml = img_arr.join('');
						var closeHtml = '<div class="close" onclick="xindm.close();" title="关闭">X</div>';
						var topHtml = '<a class="jumpToTop" href="#picView">返回顶部</a>';
						var nextHtml = nextPath == '' ? '' : '<a class="jumpToTop" href="' + nextPath + '">下一卷集</a>';
						var htmlStr = closeHtml + imgHtml + topHtml + nextHtml;
						$('#picView').html(htmlStr);

					},
					"styleSet": function() {
						var maxWidth = $('#picView')[0].offsetWidth - 6;
						$('.picBox img').css('max-width', maxWidth + 'px');
					},
					"close": function() {
						$('#container').show() + $('#picView').hide();
					}
				}
			</script>
		</body>
</html>